/*
 *  Description: BOAT XU SOFT FHSS
 *  Version: V1.00
 *  Author: boat xu
 *  Email: xifengzui@outlook.com
 *  Date: 2020-07-03 10:05:33
 *  LastEditTime: 2020-07-22 15:23:29
 */ 

#include <stdio.h>
#include <stdlib.h>
#include "bsf_types.h"
#include "random.h"
//! \brief random seed
static b_u16_t s_hwRandomSeed = 0xAA55;
static b_u8_t s_chRandomTable[] = {
                0x79,0x58,0x13,0x66,0x51,0x3A,0xD1,0x55,
                0xCA,0x25,0x77,0x8E,0x22,0xCE,0x5F,0x2A};
static b_u8_t ResetRandomTable[] = {
                0x79,0x58,0x13,0x66,0x51,0x3A,0xD1,0x55,
                0xCA,0x25,0x77,0x8E,0x22,0xCE,0x5F,0x2A};

/*! \note set random generator seed
*  \param hwSeed random seed
*  \return none
*/
void set_random_seed( b_u16_t hwSeed)
{
    b_u8_t tmp = 0;
    s_hwRandomSeed = hwSeed;
    for(tmp=0;tmp<16;tmp++)
    {
        s_chRandomTable[tmp] = ResetRandomTable[tmp];
    }
}


/*! \note get a random integer
*  \param none
*  \return random integer value
*/
b_u16_t get_random_u16( void )
{
    b_u16_t *phwResult = (b_u16_t *)&s_chRandomTable[(s_hwRandomSeed & 0x0E)];
    b_u16_t temp =  *phwResult;
    temp += s_hwRandomSeed;
     s_hwRandomSeed= s_hwRandomSeed ^ temp;
    *phwResult = temp;
    return temp;
}

/*! \note get a random byte
*  \param none
*  \return random integer value
*/
b_u8_t get_random_u8( void )
{
    return (b_u8_t)get_random_u16();
   
}
